package com.example.chat.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Admin;
import com.example.chat.beans.PointRecord;
import com.example.chat.beans.Record;
import com.example.chat.mapper.AdminMapper;
import com.example.chat.mapper.PointRecordMapper;
import com.example.chat.mapper.RecordMapper;
import com.example.chat.utils.DateUtil;
import com.example.chat.utils.Global;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class PointRecordService extends ServiceImpl<PointRecordMapper, PointRecord> {

    @Resource
    AdminMapper adminMapper;
    @Resource
    RecordMapper recordMapper;
    @Resource
    Global global;

    // 登录初始积分
    private final Integer LOGIN_POINT = 1;
    // 视频聊天初始积分
    private final Integer VIDEO_POINT = 1;

    /**
     * 计算登录积分
     */
    public void loginPoint(Admin admin) {
        // 只计算不保存
        Integer pointMultiple = global.getConfig().getPointMultiple();
        String loginDate = DateUtil.formatDate();
        int row = baseMapper.checkLoginPoint(admin.getId(), loginDate);
        if (row > 0) {
            return;
        }
        int point = LOGIN_POINT * pointMultiple;
        admin.setPoint(admin.getPoint() + point);
        PointRecord pointRecord = new PointRecord();
        pointRecord.setAdminId(admin.getId());
        pointRecord.setType(1);
        pointRecord.setPoint(point);
        pointRecord.setTotalPoint(admin.getPoint());
        pointRecord.setLoginDate(new Date());
        pointRecord.setCreateTime(System.currentTimeMillis());
        baseMapper.insert(pointRecord);
    }

    public void videoPoint1(String adminId, Integer recordId) {
        if (!StringUtils.isNumeric(adminId)) {
            return;
        }
        Admin admin = adminMapper.selectOneByAdminId(Integer.parseInt(adminId));
        if (admin == null) {
            return;
        }
        Integer pointMultiple = global.getConfig().getPointMultiple();
        int point = VIDEO_POINT * pointMultiple;
        admin.setPoint(admin.getPoint() + point);
        adminMapper.updateById(admin);
        PointRecord pointRecord = new PointRecord();
        pointRecord.setAdminId(admin.getId());
        pointRecord.setType(2);
        pointRecord.setPoint(point);
        pointRecord.setTotalPoint(admin.getPoint());
        pointRecord.setSourceId(recordId);
        pointRecord.setCreateTime(System.currentTimeMillis());
        baseMapper.insert(pointRecord);

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

        Runnable task = () -> {
            Record record = recordMapper.selectById(recordId);
            // 1已创建 2录制中 3已停止
            if (record != null && record.getStatus() == 2) {
                Admin updateAdmin = adminMapper.selectOneByAdminId(admin.getId());
                updateAdmin.setPoint(admin.getPoint() + point);
                adminMapper.updateById(updateAdmin);
                pointRecord.setPoint(point * 2);
                pointRecord.setTotalPoint(updateAdmin.getPoint());
                baseMapper.updateById(pointRecord);
            }
            executor.shutdown();
        };
        executor.schedule(task, 60000, TimeUnit.MILLISECONDS);
    }
    public void videoPoint2(Integer adminId, Integer videoId, long millis) {

    }

    public void orderPoint(Admin admin, int point, Integer orderId) {
        admin.setPoint(admin.getPoint() - point);
        PointRecord pointRecord = new PointRecord();
        pointRecord.setAdminId(admin.getId());
        pointRecord.setType(3);
        pointRecord.setPoint(-point);
        pointRecord.setTotalPoint(admin.getPoint());
        pointRecord.setSourceId(orderId);
        pointRecord.setCreateTime(System.currentTimeMillis());
        baseMapper.insert(pointRecord);
        // TODO 通知用户端
    }

    public void withdrawPoint(Admin admin, int point, String cashAccount, Integer withdrawId) {
        admin.setPoint(admin.getPoint() - point);
        PointRecord pointRecord = new PointRecord();
        pointRecord.setAdminId(admin.getId());
        pointRecord.setType(4);
        pointRecord.setPoint(-point);
        pointRecord.setTotalPoint(admin.getPoint());
        pointRecord.setCashAccount(cashAccount);
        pointRecord.setCreateTime(System.currentTimeMillis());
        pointRecord.setSourceId(withdrawId);
        baseMapper.insert(pointRecord);
        // TODO 通知用户端
    }
}
